package com.example.transit.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.service.carrier.CarrierService;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Switch;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.example.transit.R;
import com.example.transit.adapter.CollectionAdapter;
import com.example.transit.adapter.HistoryAdapter;
import com.example.transit.base.BaseFragment;
import com.example.transit.bean.CollectionBean;
import com.example.transit.bean.HistoryBean;
import com.example.transit.event.SpHistroyEvent;
import com.example.transit.ui.activity.BusRunActivity;
import com.example.transit.ui.activity.MainActivity;
import com.example.transit.utils.SPUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.text.BreakIterator;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

public class HistoryFragment extends BaseFragment {
    @BindView(R.id.rv_history)
    RecyclerView rvHistory;
    Unbinder unbinder;
    private TextView tvCollection;
    private List<HistoryBean> historyBeanList;
    private HistoryAdapter historyAdapter;
    private View footerView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder = ButterKnife.bind(this, rootView);
        return rootView;
    }


    @Override
    protected boolean isBindEventBus() {
        return false;
    }

    @Override
    public int intiLayout() {
        return R.layout.fragment_history;
    }

    @Override
    public void initView() {
        historyBeanList = new ArrayList<>();
        if (SPUtils.getArray(getContext(), SPUtils.HISTORYLIST, HistoryBean.class) != null) {
            historyBeanList = SPUtils.getArray(getActivity(), SPUtils.HISTORYLIST, HistoryBean.class);
        }
       // Log.d("may", "zhabo: " + historyBeanList.size());
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        rvHistory.setLayoutManager(layoutManager);
        historyAdapter = new HistoryAdapter(R.layout.item_history, historyBeanList);
        rvHistory.setAdapter(historyAdapter);

        rvHistory.addOnItemTouchListener(new OnItemChildClickListener() {
            @Override
            public void onSimpleItemChildClick(BaseQuickAdapter adapter, View view, int position) {

            }

            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                super.onItemChildClick(adapter, view, position);
                int itemViewId=view.getId();
                switch (itemViewId) {
                    case R.id.history_del_img:
                        historyBeanList.remove(position);
                        historyAdapter.notifyDataSetChanged();
                        SPUtils.removeArray(getActivity(), SPUtils.HISTORYLIST);
                        //Log.d("may", "zbobean: " + historyBeanList.size());
                        SPUtils.setArray(getActivity(), SPUtils.HISTORYLIST, historyBeanList);
                        break;
                    case R.id.tv_name:
                        SpHistroyEvent spHistroyEvent=new SpHistroyEvent();
                        spHistroyEvent.setHistorystr(historyBeanList.get(position).getName());
                        spHistroyEvent.setIsupline(historyBeanList.get(position).getIsupline());
                        spHistroyEvent.setLineNum(historyBeanList.get(position).getLineNum());
                        EventBus.getDefault().postSticky(spHistroyEvent);
                        Intent intent = new Intent(getActivity(), BusRunActivity.class);
                        startActivity(intent);
                        getActivity().finish();
                        break;
                }
            }
        });

    }

    @Override
    public void initData() {
        footerView = LayoutInflater.from(getActivity()).inflate(R.layout.footer_collection, null);
        historyAdapter.addFooterView(footerView);
        tvCollection = footerView.findViewById(R.id.tv_collection);
        tvCollection.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
               historyBeanList.clear();
               historyAdapter.notifyDataSetChanged();
               SPUtils.removeArray(getActivity(), SPUtils.HISTORYLIST);

            }
        });
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

}
